<template>
  <div class="dashboard">
    <el-card class="box-card overflowY" style="width: 60%;">
      <el-calendar v-model="selectedDate1" class="overflowY">
        <template #dateCell="{ data }">
          <div class="t-a-c">
            {{ data.day.split('-').slice(1).join('-') }}
            <p v-if="checkedDates.includes(data.day)">
              <el-tooltip class="item" effect="dark" content="部分空闲" placement="top">
                <i class="el-icon-time"></i>
              </el-tooltip>
            </p>
            <p v-else>
              <el-tooltip class="item" effect="dark" content="全天空闲" placement="top">
                <i class="el-icon-more"></i>
              </el-tooltip>
            </p>
          </div>
        </template>
      </el-calendar>
      <div class="titBox d-f-s fw">
        <div class="d-f-s">
          <p>注：</p>
          <i class="el-icon-time"></i>
          <p style="margin-left: 10px">代表当天部分时间段部分会议室空闲</p>
        </div>
        <div class="d-f-s" style="margin-left: 20px">
          <i class="el-icon-more"></i>
          <p style="margin-left: 10px">代表当天全天时间段部分会议室空闲</p>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="width: 40%;margin-left: 20px">
      <div slot="header" class="clearfix">
        <span>当前会议室：</span>
        <el-select v-model="meetingId" filterable placeholder="请选择" @change="changeRoom" size="small" style="width: 200px">
              <el-option 
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.status == 2" >
              </el-option>
        </el-select>
      </div>
      <div class="item-box">
        <template v-if="list.length">
          <el-card v-for="(item,index) in list" :key="index" shadow="hover"  class="item" :class="{'cp' : !item.busy }">
            <div @click="goPage(item)">
              <div class="e">
                <p>{{ item.startTime + '-' + item.endTime }} 
                  <span v-if="item.busy && item.meetingEntity.self" style="font-size:12px;color:#F56C6C;margin-left:4px">{{item.busy && item.meetingEntity.self ? '我的预约' : ''}}</span>
                  <span v-if="item.busy && item.meetingEntity.join" style="font-size:12px;color:#4BC88E;margin-left:10px">{{item.busy && item.meetingEntity.join ? '我已加入' : ''}}</span>
                </p>
                <div class="d-f-b mt-10">
                  <p>{{ item.busy ? item.meetingEntity.companyName : '暂无预约'}}</p>
                  <p v-if="item.busy">{{ item.meetingEntity.createUserName }}</p>
                </div>
              </div>
              <div class="tr" :class="!item.busy ? 'back4BC' : 'backF56'">
                {{ item.statusMessage || '可预约' }}
              </div>
            </div>
          </el-card>
        </template>
        <div v-else class="empty">
          <img src="../../assets/404_images/empty.png" alt="">
          <p >暂无数据</p>
        </div>
        
      </div>
    </el-card>
  </div>
</template>

<script>
import { getMeetingRoomCalendar, getMeetingRoomPlanCurrentDay } from "../../api/dashboard";
import { listPage } from "../../api/conferenceRoom";
export default {
  data() {
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const day = String(today.getDate()).padStart(2, '0');
    const todayStr = `${year}-${month}-${day}`;
    return {
      checkedDates: [],// 需要打钩的日期数组
      selectedDate1: todayStr,
      selectedDate: todayStr,// 当前选中的日期，初始化为今天
      options: [],
      meetingId: '',
      list: []
    };
  },
  watch: {
    selectedDate1(newVal) {
      const dateObj = new Date(newVal);
      const year = dateObj.getFullYear();
      const month = String(dateObj.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需要加 1
      const day = String(dateObj.getDate()).padStart(2, '0'); // 确保日期是两位数
      const formattedDate = `${year}-${month}-${day}`;
      this.selectedDate = formattedDate
      this.getMeetingRoomCalendar(),
      this.getMeetingRoomPlanCurrentDay()
    }
  },
  mounted() {
    this.getMeetingRoomCalendar()
    this.getList()
    // this.getMeetingRoomPlanCurrentDay()
  },
  methods: {
    // 判断时间是否在当前之前
    isBeforeCurrentTime(dateStr) {
      const inputDate = new Date(dateStr);
      const currentDate = new Date();
      return inputDate < currentDate;
    },
    isYYYYMMDDFormat(dateStr) {
      const regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
      return regex.test(dateStr);
    },
    getYYYY(newVal) {
      const dateObj = new Date(newVal);
      const year = dateObj.getFullYear();
      const month = String(dateObj.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始，需要加 1
      const day = String(dateObj.getDate()).padStart(2, '0'); // 确保日期是两位数
      return `${year}-${month}-${day}`;
    },
    // 处理日历显示
    getMeetingRoomCalendar(date) {
      getMeetingRoomCalendar({
        moth: date ? date : this.selectedDate
      }).then(res => {
        if(res.code === 200) {
          this.checkedDates = res.data.filter(item => item.busy === true).map(item => item.time);
        }else this.$message.error(res.message)
      })
    },
    // 会议室列表
    getList(roomName) {
      let params = {
        page: 1,
        pageSize: 9999,
        roomName: roomName || "",
      };
      listPage(params).then((res) => {    
        if (res.code == 200) {
          this.options = res.data.data.map(e => {
            return {
              label: e.roomName,
              value: e.id,
              status: e.status,
            }
          })
          if(this.meetingId == '') this.meetingId = this.options[0].value
          this.getMeetingRoomPlanCurrentDay()
        } else this.$message.error(res.message);
      });
    },
    changeRoom(e) {
      this.meetingId = e
      console.log(this.selectedDate,666);
      this.getMeetingRoomPlanCurrentDay()
    },
    getMeetingStatus(item) {
        const currentDate = new Date();
        const startDate = new Date(item.startDate.replace(/-/g, '/'));        
        const endDate = new Date(item.endDate.replace(/-/g, '/'));
        let msg = ''
        if(!item.busy) msg = '可预约'
        if(item.busy && currentDate < startDate) msg = '已预约'
        if(item.busy && currentDate >= startDate && currentDate < endDate) msg = '进行中'
        // if(item.busy && item.meetingEntity.self) msg = '我的预约'
        // if(item.busy && item.meetingEntity.join) msg = '已加入'
        if(item.busy && currentDate >= endDate) msg = '已结束'
        return msg
    },
    // 获取当天时间排期
    getMeetingRoomPlanCurrentDay() {  
      this.list = []    
      getMeetingRoomPlanCurrentDay({
        date: this.selectedDate,
        meetingRoomId: this.meetingId
      }).then((res) => {    
        if (res.code == 200) {
          if(!res.data.length) return
          let list = res.data.map(item => {
            return {
              ...item,
              startDate: this.isYYYYMMDDFormat(this.selectedDate1) ? this.selectedDate1 + ' ' + item.startTime + ':00' : this.getYYYY(this.selectedDate1) + ' ' + item.startTime + ':00',
              endDate: this.isYYYYMMDDFormat(this.selectedDate1) ? this.selectedDate1 + ' ' + item.endTime + ':00' : this.getYYYY(this.selectedDate1) + ' ' + item.endTime + ':00',
            }
          })
          this.list = list.map(e => {
            return {
              ...e,
              statusVal: this.getMeetingStatus(e)
            }
          })
        } else this.$message.error(res.message);
      })
    },
    goPage(item) {
      if(item.busy) return
      if(this.isBeforeCurrentTime(item.startDate)) return
      this.$router.push({ name: "ConferenceMng", params: {date:[item.startDate,item.endDate,]} })
    }
  }
};
</script>

<style lang="scss" scoped>
.dashboard {
  padding: 20px;
  display: flex;
  height: calc(100vh - 50px);
  justify-content: flex-start;
  ::v-deep.el-calendar-table td.is-today{
    color: #F56C6C !important;
  }
  ::v-deep.el-calendar-table td.is-selected{
    background-color: #4BC88E !important;
  }
  ::v-deep.el-calendar-table .el-calendar-day:hover {
    background-color: #4BC88E !important;
  }
  .titBox{
    padding: 0 20px;
    p{
      color: #F56C6C;
    }
    
  }
  ::v-deep.el-card__body{
    padding: 5px 0 !important;
  }
  ::v-deep.el-calendar__header{
    padding: 12.5px 20px !important;
  }
  ::v-deep.el-card__header{
    padding: 13px 20px !important;
  }
  .t-a-c {
    text-align: center;
  }
  .overflowY{
    overflow-y: auto;  
  }
  .item-box{
    padding: 0 10px;
    padding-bottom: 120px;
    height: calc(100vh - 50px);
    overflow-y: auto;  
    .item{
      margin-top: 10px;
      padding: 5px 10px;
      position: relative;
      position: relative;
      .e{
        p{
          margin: 0;
        }
      }
      .tr{
        position: absolute;
        top: 0;
        right: 0;
        padding: 6px 8px 4px 8px;
        font-size: 12px;
        color: #fff;
        border-bottom-left-radius: 4px;
      }
    }
  }
  .empty{
    text-align: center;
    margin-top: 80px;
  }
  .back4BC{
    background-color: #4BC88E;
  }
  .backF56{
    background-color: #F56C6C;
  }
  .mt-10{
    margin-top: 10px;
  }
  .cp{
    cursor: pointer;
  }
  .d-f-b{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .d-f-s{
    display: flex;
    align-items: center;
    justify-content: flex-start;
  }
  .fw{
    flex-wrap: wrap;
  }
}
</style>
